package String;

public class _696_CountBinarySubstrings {
    //hint if bytes[i]!=bytes[i-1],then count++,and judge if bytes[i+1]==bytes[i] && bytes[i-2]==bytes[i-1],
    //calculate until don't satisfy the conditions
    public int countBinarySubstrings(String s) {
        int count = 0;
        char[] bytes = s.toCharArray();
        char prev = bytes[0];
        for(int i=1;i<bytes.length;i++) {
            char temp = bytes[i];
            int left =0,right = 0;
            if (temp != prev) {
                count++;
                left = i-2;
                right = i+1;
                while (left > -1 && right < bytes.length && bytes[right] == temp) {
                    if (bytes[right] != bytes[left]) {
                        count++;
                        left--;
                        right++;
                    } else {
                        break;
                    }
                }
            }
            prev = temp;
        }
        return count;
    }
}
